#!/bin/bash
# 使用说明

# 下载
file_downloader(){
  file_download_url=$1
  echo "[Debug] file_download_url = $file_download_url"
  file_name=`echo $file_download_url | awk -F'/' '{print $NF}'`
  # file_name=`echo $file_download_url | rev | cut -d '/' -f 1 | rev`
  echo "[Debug] file_name = $file_name"
  # save_file_path="$HOME/Downloads/$file_name"
  save_file_path="$HOME/Downloads/"
  echo "[Debug] save_file_path = $save_file_path"

  # 判断是否文件存在
  if [[ ! -f "$save_file_path" ]]; then
    echo "[Debug]文件 $save_file_path 不存在！"
    if [[  -x "$(which axel)" ]]; then
      echo "[Debug]使用axel下载"
      cmd_str="axel -o $save_file_path$file_name  $file_download_url"
      echo "[Debug] cmd_st = $cmd_str"
      $cmd_str
    elif [[  -x "$(which wget)" ]]; then 
      echo "[Debug]使用wget下载"
      # wget https://evermeet.cx/ffmpeg/ffmpeg-113507-gf6b7b473d4.zip 
      # wget -c "$file_download_url"
      # wget -c -O  "$save_file_path" "$file_download_url"
      # wget -b -c "$file_download_url" # 后台下载+断点续传
      cmd_str="wget -c -P $save_file_path  -O  $file_name $file_download_url"
      echo "[Debug] cmd_st = $cmd_str"
      $cmd_str
    else
      echo "[Debug]使用curl下载"
      # curl -C -# -R -O -I "$file_download_url" # 显示下载进度、断点下载
      curl -C -# -R  -I -o "$save_file_path" "$file_download_url" 
      # -O 将文件下载到当前目录下
      # -o 将文件 下载到 download 目录下，并重命名为 test.txt
      # -# 显示下载进度
      # -C 断点续传
      # [Linux 中三个下载工具-wget curl axel - 知乎](https://zhuanlan.zhihu.com/p/451426230)
    fi
  else 
    echo "[Debug] 文件 $save_file_path 存在！"
  fi
  echo ${save_file_path}
}


# 解压到指定位置
extract2path(){
  target_file_path=$1
  extract_file_path=$2
  extract_need_move_file_path=$3
  # 判断是什么文件，使用不同解压命令解压
  ext_name=`echo $target_file_path | awk -F'/' '{print $NF}' | awk -F'.' '{print $NF}' `
  echo "[Debug] ext_name = $ext_name"
  # 解压
  case $ext_name in
    'zip')   echo '*.zip'
        # unzip -d ~/.local/bin ./ffmpeg-113507-gf6b7b473d4.zip
        unzip -d "$HOME/Downloads"  "$target_file_path"
      ;;
    '.tar.gz' | 'gz'  ) 
        echo "[Debug] *.tar.gz | *.gz "
        # 解压
        cmd_str="tar -zvxf $target_file_path -C $HOME/Downloads"
        echo "[Debug] cmd_st = $cmd_str"
        $cmd_str
      ;;
    *) echo '[Debug] default' 
      ;;
  esac
  # 解压后 移动到指定位置
  # ls -al ${target_file_path}
  # 判读目标路径是否存在
  # ls -al ${extract_file_path}
  if [[ ! -d "${extract_file_path}" ]]; then
    #statements
    echo "[Debug] $extract_file_path 不存在"
    mkdir -p ${extract_file_path}
  else
    echo "[Debug] $extract_file_path 存在"
  fi
  move_path="$HOME/Downloads/${extract_need_move_file_path}" 
  echo "[Debug] move_path = $move_path"
  mv $move_path ${extract_file_path}

}

# 传入参数初步处理 
# 防止传入的值不对
case $1 in
    'm3u8dl') 
        file_download_url="https://gh.api.99988866.xyz/https://github.com/nilaoda/N_m3u8DL-RE/releases/download/v0.2.0-beta/N_m3u8DL-RE_Beta_linux-x64_20230628.tar.gz"
        file_downloader ${file_download_url}
        file_name=`echo ${file_download_url} | awk -F'/' '{print $NF}'`
        target_file_path="$HOME/Downloads/$file_name"
        extract_file_path="$HOME/.local/bin"
        extract2path "${target_file_path}"  "$extract_file_path" "N_m3u8DL-RE_Beta_linux-x64/*"
        chmod +x "$extract_file_path/N_m3u8DL-RE"
        ;;
    'BBDown') 
        file_download_url="https://gh.api.99988866.xyz/https://github.com/nilaoda/BBDown/releases/download/1.6.1/BBDown_1.6.1_20230818_linux-x64.zip"
        file_downloader ${file_download_url}
        file_name=`echo ${file_download_url} | awk -F'/' '{print $NF}'`
        target_file_path="$HOME/Downloads/$file_name"
        extract_file_path="$HOME/.local/bin"
        extract2path "${target_file_path}"  "$extract_file_path" "BBDown"
        chmod +x "$extract_file_path/BBDown"
        ;;
    'ffmpeg') 
        file_downloader "https://github.com/nilaoda/N_m3u8DL-RE/releases/download/v0.2.0-beta/N_m3u8DL-RE_Beta_linux-x64_20230628.tar.gz"
        ;;
    'localsend')
        file_download_url="https://gh.api.99988866.xyz/https://github.com/localsend/localsend/releases/download/v1.14.0/LocalSend-1.14.0-linux-x86-64.AppImage"
        file_downloader ${file_download_url}
        file_name=`echo ${file_download_url} | awk -F'/' '{print $NF}'`
        target_file_path="$HOME/Downloads/$file_name"
        extract_file_path="$HOME/.local/bin"
        # extract2path "${target_file_path}"  "$extract_file_path" "BBDown"
        mv $target_file_path $extract_file_path
        chmod +x "$extract_file_path/BBDown"
        ;;
    *) echo "请输入m3u8dl、BBDown、localsend"
      ;;
esac


# github 
# https://gh.api.99988866.xyz/

# ffmpeg
# https://github.com/FFmpeg/FFmpeg

# N_m3u8DL-RE
# https://github.com/nilaoda/N_m3u8DL-RE/releases/download/v0.2.0-beta/N_m3u8DL-RE_Beta_osx-x64_20230628.tar.gz
# https://github.com/nilaoda/N_m3u8DL-RE/releases/download/v0.2.0-beta/N_m3u8DL-RE_Beta_linux-x64_20230628.tar.gz

# BBDown
# https://github.com/nilaoda/BBDown/releases/download/1.6.1/BBDown_1.6.1_20230818_osx-x64.zip
# https://github.com/nilaoda/BBDown/releases/download/1.6.1/BBDown_1.6.1_20230818_linux-x64.zip

# localsend
# https://github.com/localsend/localsend/releases/download/v1.14.0/LocalSend-1.14.0-linux-x86-64.AppImage
